//
//  ManuItem.m
//  Unity-iPhone
//
//  Created by Karl on 2017/3/28.
//
//

#import "ManuItem.h"

@implementation ManuItem{
    UIButton * _btn;
    UILabel *_lab;
    NSString *_title;
    NSString *_image;
    NSString *_url;
    BOOL  _inApp;
    CGSize _size;
}


- (instancetype)initWithDic:(NSDictionary *)dic size:(CGSize)size{
    self = [super init];
    if (self) {
        _title = dic[@"title"];
        _image = dic[@"image"];
        _url = dic[@"url"];
        _size = size;
        _inApp = [dic[@"in_app"] boolValue];
        
        _btn = [UIButton new];
        [_btn setBackgroundImage:[UIImage imageNamed:_image] forState:UIControlStateNormal];
        [_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btn];
        
        _lab = [UILabel new];
        [_lab setTextAlignment:NSTextAlignmentCenter];
        [_lab setText:_title];
        [_lab setTextColor:[UIColor whiteColor]];
        [_lab setFont:[UIFont systemFontOfSize:9]];
        [_lab setNumberOfLines:0];
        [_lab sizeToFit];
        //        if ([title isEqualToString:@""]) {
        //
        //        }else if ([title isEqualToString:@""]){
        //
        //        }
        //        _lab.adjustsFontSizeToFitWidth = YES;
        [_lab setTextAlignment:NSTextAlignmentCenter];
        [self addSubview:_lab];
        
        self.itemHeight = size.width + _lab.bounds.size.height;
    }
    return self;

}

//- (instancetype)initWithTitle:(NSString *)title image:(NSString *)image url:(NSString *)url size:(CGSize)size{
//    }

- (void)layoutSubviews{
    [super layoutSubviews];
    
    _btn.left = 0;
    _btn.top = 0;
    _btn.width = _size.width;
    _btn.height = _size.width;
    
    _lab.centerX = _btn.centerX;
    _lab.top = _btn.bottom;
//    _lab.width = _size.width;
//    _lab.height = _size.height- _size.width;
}

- (void)btnAction:(id)sender{
    NSLog(@"menu item %@ click",_title);
    if (_url.length) {
        [[NativeViewManager sharedManager] showWebWithURL:_url inApp:_inApp];
    }else{
        if ([_delegate respondsToSelector:@selector(manuItemAction:)]) {
            [_delegate manuItemAction:_title];
        }
    }
    
}

@end
